/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package workforce.limousine.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import limousine.vo.Driver;
import limousine.vo.TaxiTariff;

/**
 *
 * @author krisada
 */
public class TaxiTariffService implements Service {

    public DataSource dataSource = null;
    private int limit = 20;

    public TaxiTariffService() {
        System.out.println("Initial TaxiTariff");
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public boolean save(Object obj) {
        try {
            Connection connection = dataSource.getConnection();
            System.out.println("Save car");
            TaxiTariff o = (TaxiTariff) obj;
            int count = 0;
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT INTO taxi_tariff(TAXI_RATE_ID,DISTINATION,TAXI_SIZE,ONE_WAY,ROUND_TRIP,CREATE_BY,CREATE_DATE,UPDATE_BY,UPDATE_DATE,TAXI_TARIFF_ORDER ) ");
            sql.append("VALUES (?,?,?,?,?,?,CURRENT_TIMESTAMP,?,CURRENT_TIMESTAMP,?)");
            PreparedStatement psmt = connection.prepareStatement(sql.toString());
            psmt.setString(1, o.getTaxiRateId());
            psmt.setString(2, o.getDistination());
            psmt.setInt(3, o.getTaxiSize());
            psmt.setInt(4, o.getOneWay());
            psmt.setInt(5, o.getRoundTrip());
            psmt.setString(6, o.getCreateBy());
            psmt.setString(7, o.getUpdateBy());
            psmt.setInt(8, o.getTaxiTariffOrder());

            count = psmt.executeUpdate();
            psmt.close();
            if (psmt != null) {
                psmt = null;
            }

            connection.close();
            if (connection != null) {
                connection = null;
            }
            return (count > 0) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean update(Object obj) {
        try {
            Connection connection = dataSource.getConnection();
            //System.out.println("Update car");
            TaxiTariff o = (TaxiTariff) obj;
            int count = 0;
            StringBuilder sql = new StringBuilder();
            sql.append("UPDATE taxi_tariff ");
            sql.append("SET DISTINATION=?,TAXI_SIZE=?,ONE_WAY=?,ROUND_TRIP=?,UPDATE_BY=?,UPDATE_DATE=CURRENT_TIMESTAMP,TAXI_TARIFF_ORDER=?  ");
            sql.append("where TAXI_RATE_ID=? ");
            PreparedStatement psmt = connection.prepareStatement(sql.toString());
            psmt.setString(1, o.getDistination());
            psmt.setInt(2, o.getTaxiSize());
            psmt.setInt(3, o.getOneWay());
            psmt.setInt(4, o.getRoundTrip());
            psmt.setString(5, o.getUpdateBy());
            psmt.setInt(6, o.getTaxiTariffOrder());
            psmt.setString(7, o.getTaxiRateId());
            count = psmt.executeUpdate();
            psmt.close();
            if (psmt != null) {
                psmt = null;
            }

            connection.close();
            if (connection != null) {
                connection = null;
            }
            return (count > 0) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public boolean remove(Object obj) {
        try {
            Connection conn = dataSource.getConnection();
            System.out.println("remove car");
            StringBuilder s = new StringBuilder();
            s.append("delete from taxi_tariff where TAXI_RATE_ID =? ");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement pstm = conn.prepareStatement(s.toString());
            pstm.setString(1, (String) obj);
            int ret = pstm.executeUpdate();
            pstm.close();
            if (pstm != null) {
                pstm = null;
            }

            conn.close();
            if (conn != null) {
                conn = null;
            }
            return (ret == 1) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public Object findById(String id) {
        try {
            Connection conn = dataSource.getConnection();
            System.out.println("FindById");
            StringBuilder s = new StringBuilder();
            s.append("SELECT ");
            s.append("IFNULL(A.TAXI_RATE_ID, '-') AS TAXI_RATE_ID,");
            s.append("IFNULL(A.DISTINATION, '-') AS DISTINATION,");
            s.append("IFNULL(A.TAXI_SIZE, 0) AS TAXI_SIZE,");
            s.append("IFNULL(A.ONE_WAY, 0) AS ONE_WAY,");
            s.append("IFNULL(A.ROUND_TRIP, 0) AS ROUND_TRIP,");
            s.append("IFNULL(A.CREATE_BY, '-') AS CREATE_BY,");
            s.append("IFNULL(A.CREATE_DATE, CURRENT_TIMESTAMP) AS CREATE_DATE,");
            s.append("IFNULL(A.UPDATE_BY, '-') AS UPDATE_BY,");
            s.append("IFNULL(A.UPDATE_DATE, CURRENT_TIMESTAMP) AS UPDATE_DATE,");
            s.append("IFNULL(A.TAXI_TARIFF_ORDER, 0) AS TAXI_TARIFF_ORDER ");
            s.append("FROM taxi_tariff A WHERE A.TAXI_RATE_ID =? ");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement pstm = conn.prepareStatement(s.toString());
            pstm.setString(1, id);
            ResultSet rs = pstm.executeQuery();
            TaxiTariff o = new TaxiTariff();
            if (rs.next()) {
                o.setTaxiRateId(rs.getString("TAXI_RATE_ID"));
                o.setDistination(rs.getString("DISTINATION"));
                o.setTaxiSize(rs.getInt("TAXI_SIZE"));
                o.setOneWay(rs.getInt("ONE_WAY"));
                o.setRoundTrip(rs.getInt("ROUND_TRIP"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(new Date(rs.getDate("CREATE_DATE").getTime()));
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(new Date(rs.getDate("UPDATE_DATE").getTime()));
                o.setTaxiTariffOrder(rs.getInt("TAXI_TARIFF_ORDER"));
            }
            rs.close();
            if (rs != null) {
                rs = null;
            }

            pstm.close();
            if (pstm != null) {
                pstm = null;
            }

            conn.close();
            if (conn != null) {
                conn = null;
            }
            return o;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public List find(Object[] obj) {
        List<Driver> list = new ArrayList<Driver>();
        try {
            Connection connection = dataSource.getConnection();
            System.out.println("Save Driver");
            connection.close();
            return list;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public List<TaxiTariff> search(int offset, Object[] certiria) {
        System.out.println("CarService search...");
        List<TaxiTariff> list = new ArrayList<TaxiTariff>();
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            StringBuilder s = new StringBuilder();
            s.append("SELECT ");
            s.append("IFNULL(A.TAXI_RATE_ID, '-') AS TAXI_RATE_ID,");
            s.append("IFNULL(A.DISTINATION, '-') AS DISTINATION,");
            s.append("IFNULL(A.TAXI_SIZE, 0) AS TAXI_SIZE,");
            s.append("IFNULL(A.ONE_WAY, 0) AS ONE_WAY,");
            s.append("IFNULL(A.ROUND_TRIP, 0) AS ROUND_TRIP,");
            s.append("(SELECT IFNULL(B.USER_NAME, '-') FROM users B WHERE B.id = A.CREATE_BY ) AS CREATE_BY,");
            s.append("IFNULL(A.CREATE_DATE, CURRENT_TIMESTAMP) AS CREATE_DATE,");
            s.append("(SELECT IFNULL(B.USER_NAME, '-') FROM users B WHERE B.id = A.UPDATE_BY ) AS UPDATE_BY,");
            s.append("IFNULL(A.UPDATE_DATE, CURRENT_TIMESTAMP) AS UPDATE_DATE,");
            s.append("IFNULL(A.TAXI_TARIFF_ORDER, 0) AS TAXI_TARIFF_ORDER ");            
            s.append("FROM taxi_tariff A WHERE A.DISTINATION LIKE ? ");
            s.append("LIMIT ? OFFSET ?");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement stmt = conn.prepareStatement(s.toString());
            String name = (String) certiria[0];
            name = (name != null) ? name.replace('*', '%') : "";
            if (name.indexOf("%") == -1) {
                name = "%" + name + "%";
            }
            stmt.setString(1, name);
            stmt.setInt(2, limit);
            stmt.setInt(3, offset);
            ResultSet rs = stmt.executeQuery();
   
            System.out.println("loop---limit["+ limit+"]["+ offset+"]");
            while (rs.next()) {
                
                TaxiTariff o = new TaxiTariff();
                o.setTaxiRateId(rs.getString("TAXI_RATE_ID"));
                o.setDistination(rs.getString("DISTINATION"));
                o.setTaxiSize(rs.getInt("TAXI_SIZE"));
                o.setOneWay(rs.getInt("ONE_WAY"));
                o.setRoundTrip(rs.getInt("ROUND_TRIP"));
                o.setCreateBy(rs.getString("CREATE_BY"));
                o.setCreateDate(new Date(rs.getDate("CREATE_DATE").getTime()));
                o.setUpdateBy(rs.getString("UPDATE_BY"));
                o.setUpdateDate(new Date(rs.getDate("UPDATE_DATE").getTime()));
                o.setTaxiTariffOrder(rs.getInt("TAXI_TARIFF_ORDER"));
                list.add(o);
                System.out.println("loop------------");
            }
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    conn = null;
                } catch (Exception ex) {
                }
            }
        }
        return list;
    }

    public boolean removeAll(String[] ids) {
        try {
            Connection conn = dataSource.getConnection();
            System.out.println("Save Driver");
            StringBuilder s = new StringBuilder();
            s.append("DELETE FROM taxi_tariff WHERE TAXI_RATE_ID = ?");
            System.out.println("sql[" + s.toString() + "]");
            Statement pstm = conn.createStatement();
            for (int i = 0; i < ids.length; i++) {
                pstm.addBatch("DELETE FROM taxi_tariff WHERE TAXI_RATE_ID = '" + ids[i] + "'");
            }
            int ret[] = pstm.executeBatch();
            pstm.close();
            if (pstm != null) {
                pstm = null;
            }
            conn.close();
            if (conn != null) {
                conn = null;
            }
            return (ret.length == ids.length) ? true : false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public int searchCount(int offset, Object[] certiria) {
        int count = 0;
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            StringBuilder s = new StringBuilder();
            s.append("SELECT COUNT(*) FROM taxi_tariff WHERE TAXI_RATE_ID LIKE ?");
            System.out.println("sql[" + s.toString() + "]");
            PreparedStatement stmt = conn.prepareStatement(s.toString());
            String name = (String) certiria[0];
            name = (name != null) ? name.replace('*', '%') : "";
            stmt.setString(1, name);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                    conn = null;
                } catch (Exception ex) {
                }
            }
        }
        return count;
    }

    public Object findById(String uid, String id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
